package Lono::Recipe::Builder::Form;

use strict;
use warnings;

use Lono::Recipe::Builder;

sub new {
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

sub load {
    my ($self, $fields) = @_;
    
    # XXX: we should be able to just auto-set recipe.*... 
    my $builder = Lono::Recipe::Builder->new;
    $builder->set_name( $fields->{'recipe.name'} );
    $builder->set_preparation( $fields->{'recipe.preparation'} );
    $builder->set_description( $fields->{'recipe.description'} );
    $builder->set_source( $fields->{'recipe.source'} );
    $builder->set_url( $fields->{'recipe.url'} );

    # XXX: this will perform an ascii sort and won't really preserve the order
    foreach my $ing_key ( sort grep /recipe\.ingredient/, keys( %{$fields} ) ) {
        $builder->add_ingredient( $fields->{$ing_key} );
    }

    my $recipe = $builder->build_recipe;
   
    return $recipe; 
}

1;
